package com.pearadmin.modules.sys.controller.work;

import com.github.pagehelper.PageInfo;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.modules.sys.domain.WorkOrder;
import com.pearadmin.modules.sys.service.IWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 工单管理Controller
 *
 * @author Mr.Ling
 * @date 2021-12-18
 */
@RestController
@RequestMapping("/work/order")
public class WorkOrderController extends BaseController {

    private String prefix = "work/order" ;

    @Autowired
    private IWorkOrderService workOrderService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/work/order/main','work:order:main')")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询工单管理列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/work/order/data','work:order:data')")
    public ResultTable list(@ModelAttribute WorkOrder workOrder, PageDomain pageDomain) {
        PageInfo<WorkOrder> pageInfo = workOrderService.selectWorkOrderPage(workOrder, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增工单管理
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/work/order/add','work:order:add')")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存工单管理
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/work/order/add','work:order:add')")
    public Result save(@RequestBody WorkOrder workOrder) {
        return decide(workOrderService.insertWorkOrder(workOrder));
    }

    /**
     * 修改工单管理
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/work/order/edit','work:order:edit')")
    public ModelAndView edit(String id, ModelMap mmap) {
        WorkOrder workOrder = workOrderService.selectWorkOrderById(id);
        mmap.put("workOrder", workOrder);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存工单管理
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/work/order/edit','work:order:edit')")
    public Result update(@RequestBody WorkOrder workOrder) {
        return decide(workOrderService.updateWorkOrder(workOrder));
    }

    /**
     * 删除工单管理
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/work/order/remove','work:order:remove')")
    public Result batchRemove(String ids) {
        return decide(workOrderService.deleteWorkOrderByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/work/order/remove','work:order:remove')")
    public Result remove(@PathVariable("id") String id) {
        return decide(workOrderService.deleteWorkOrderById(id));
    }
}
